package vpncontroller

import (
	"context"
	"encoding/json"
	"vpn-management-server/logger"
	"vpn-management-server/network"
	"vpn-management-server/security/code"
	"vpn-management-server/src/dao/vpndao"
	"vpn-management-server/src/model/ugroupmodel"
	"vpn-management-server/src/model/usermodel"
	"vpn-management-server/src/model/vpnmodel"
	"vpn-management-server/src/service/vpnservice"
)

func AddVpnServerInfo(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if VpnServerInfo, ok := request.GetModel().(*vpnmodel.VpnServerInfo); ok {
		err := vpndao.AddVpnServerInfo(VpnServerInfo)
		if err != nil {
			logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "add vpn server info fail")
			response.Code = code.BAD_REQUEST
			response.ErrorMsg = "add vpn server info fail"
			return
		}
		logger.Logger.Info("add vpn server info success")
		response.Code = code.SUCCESS
		return
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to VpnServerInfo fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to VpnServerInfo fail"
		return
	}
}

func DeleteVpnServerInfo(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if VpnServerID, ok := request.GetModel().(*vpnmodel.VpnServerNameInfo); ok {
		err := vpndao.DeleteVpnServerInfoByID(VpnServerID.ID)
		if err != nil {
			logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "delete vpn server info fail")
			response.Code = code.BAD_REQUEST
			response.ErrorMsg = "delete vpn server info fail"
			return
		}
		logger.Logger.Info("delete vpn server info success")
		response.Code = code.SUCCESS
		return
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to VpnServerNameInfo fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to VpnServerNameInfo fail"
		return
	}
}

func GetUGroupVpnServerInfosByPage(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if PageWithID, ok := request.GetModel().(*ugroupmodel.PageWithID); ok {
		VpnServerInfos := vpndao.GetUGroupVpnServerInfosByPage(PageWithID.UGroupID, PageWithID.PageNo, PageWithID.PageSize)
		if VpnServerInfos == nil {
			logger.Logger.Warn("vpn server info is empty")
			VpnServerInfos = []*vpnmodel.VpnServerInfo{}
		}
		var resData struct {
			VpnServerInfos []*vpnmodel.VpnServerInfo
			Count          int
		}
		logger.Logger.Info("get vpn server info success")
		resData.VpnServerInfos = VpnServerInfos
		resData.Count = vpndao.GetVpnServerInfosCount(PageWithID.UGroupID)
		data, _ := json.Marshal(resData)
		response.Code = code.SUCCESS
		response.Data = data
		return
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to PageWithID fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to PageWithID fail"
		return
	}
}

func GetVpnServerInfoByUserName(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if UserNameInfo, ok := request.GetModel().(*usermodel.UserNameInfo); ok {
		VpnServerInfos := vpnservice.GetVpnServerInfoByUserName(UserNameInfo.Name)
		if VpnServerInfos == nil {
			logger.Logger.Warn("vpn server info is empty")
			VpnServerInfos = []*vpnmodel.VpnServerInfo{}
		}
		logger.Logger.Info("get vpn server infos success")

		data, _ := json.Marshal(VpnServerInfos)
		logger.Logger.Debug(string(data))
		response.Code = code.SUCCESS
		response.Data = data
		return
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to UserNameInfo fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to UserNameInfo fail"
		return
	}
}

func GetAllVpnServerNameInfos(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if UGroupID, ok := request.GetModel().(*vpnmodel.UGroupID); ok {
		VpnServerNameInfos := vpndao.GetAllVpnServerNameInfosByUGroupID(UGroupID.ID)
		if VpnServerNameInfos == nil {
			logger.Logger.Warn("vpn server name info is empty")
			VpnServerNameInfos = []*vpnmodel.VpnServerNameInfo{}
		}
		logger.Logger.Info("get vpn server name infos success")
		data, _ := json.Marshal(VpnServerNameInfos)
		response.Code = code.SUCCESS
		response.Data = data
		return
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to UGroupID fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to UGroupID fail"
		return
	}
}

func GetVpnServerInfoByID(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	if VpnServerNameInfo, ok := request.GetModel().(*vpnmodel.VpnServerNameInfo); ok {
		vpnServerInfo := vpndao.GetVpnServerInfoByID(VpnServerNameInfo.ID)
		// 这个如果是空 那是没找到 要报error
		if vpnServerInfo == nil {
			logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "get vpn server info by id fail")
			response.Code = code.BAD_REQUEST
			response.ErrorMsg = "get vpn server info by id fail"
			return
		}
		logger.Logger.Info("get vpn server info by id success")
		data, _ := json.Marshal(vpnServerInfo)
		response.Code = code.SUCCESS
		response.Data = data
		return
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to VpnServerNameInfo fail")
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "transform interface to VpnServerNameInfo fail"
		return
	}
}
